// pages/personal/submitted/submitted.js
const app = getApp()
const router = require('../../../config').router;
const uploadImage = require('../../../config').uploadImage;

Page({

  /**  
   * 页面的初始数据  
   */
  data: {
    image: [],
    type:true
    // files: [],
    // date: '2016-09-01',
  },
  /**  
   * 生命周期函数--监听页面加载  
   */
  onLoad: function (options) {
    // function getNowFormatDate() {
    //     var date = new Date();
    //     var seperator1 = "-";
    //     var year = date.getFullYear();
    //     var month = date.getMonth() + 1;
    //     var strDate = date.getDate();
    //     if (month >= 1 && month <= 9) {
    //         month = "0" + month;
    //     }
    //     if (strDate >= 0 && strDate <= 9) {
    //         strDate = "0" + strDate;
    //     }
    //     var currentdate = year + seperator1 + month + seperator1 + strDate;
    //     return currentdate;
    // }
    // var date = getNowFormatDate();
    // this.setData({
    //     date: date
    // });
    var that = this
    var contactName = wx.getStorageSync('contactName')
    var contactPassword = wx.getStorageSync('contactPassword')
    if (contactName != '' && contactPassword != '') {
      that.setData({
        type:false
      });
    }
  },
  /**
   * 上传图片
   */
  uploadClick: function() {
    var that = this;
    wx.chooseImage({
      count: 5, //最多可以选择的图片总数
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        //启动上传等待中...
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        })
        var uploadImgCount = 0;
        for (var i = 0, h = tempFilePaths.length; i < h; i++) {
          wx.uploadFile({
            //url: 'http://47.107.80.250:18082/mobile/file/uploadImage',
            url:uploadImage,
            filePath: tempFilePaths[i],
            name: 'photoFile',
            formData: {
                'imgIndex': i
            },
            header: {
                "Content-Type": "multipart/form-data"
            },
            success: function(res) {
                uploadImgCount++;
                var data = res.data;
                console.log(data)
                var img = {
                    img: data
                }
                that.data.image.push(img)
                that.setData({
                    image: that.data.image
                });
                console.log(that.data.image)
                //如果是最后一张,则隐藏等待中
                if (uploadImgCount == tempFilePaths.length) {
                    wx.hideToast();
                }
            },
            fail: function(res) {
              console.log(res)
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false,
                success: function(res) {}
              })
              }
            });
          }
        }
    });
  },
  // fileClick: function (e) {
  //   var that = this;
  //   wx.chooseMessageFile({
  //       count: 10,
  //       type: 'file',
  //       success (res) {
  //           const tempFiles = res.tempFiles
  //           //启动上传等待中...
  //           wx.showToast({
  //               title: '正在上传...',
  //               icon: 'loading',
  //               mask: true,
  //               duration: 10000
  //           })
  //           var tempFilesCount = 0;
  //           for (var i = 0, h = tempFiles.length; i < h; i++) {
  //               wx.uploadFile({
  //                   //url: 'http://47.107.80.250:18082/mobile/file/uploadImage',
  //                   url:uploadImage,
  //                   filePath: tempFiles[i].path,
  //                   name: 'photoFile',
  //                   formData: {
  //                       'imgIndex': i
  //                   },
  //                   header: {
  //                       "Content-Type": "multipart/form-data"
  //                   },
  //                   success: function(res) {
  //                       var name = tempFiles[tempFilesCount].name;
  //                       tempFilesCount++;
  //                       var src = res.data;
  //                       console.log(src)
  //                       var arr = {
  //                           name: name,
  //                           src:src
  //                       }
  //                       that.data.files.push(arr)
  //                       that.setData({
  //                           files: that.data.files
  //                       });
  //                       console.log(that.data.files)
  //                       //如果是最后一个文件,则隐藏等待中
  //                       if (tempFilesCount == tempFiles.length) {
  //                           wx.hideToast();
  //                       }
  //                   },
  //                   fail: function(res) {
  //                       console.log(res)
  //                       wx.hideToast();
  //                       wx.showModal({
  //                           title: '错误提示',
  //                           content: '上传文件失败',
  //                           showCancel: false,
  //                           success: function(res) {}
  //                       })
  //                   }
  //               });
  //           }
  //       }
  //   })
  // },
  delmsg: function (e) {
      console.log(e)
      var that = this
      var dex = e.currentTarget.dataset.del
      wx.showModal({
          title: '删除图片',
          content: '确定要删除该图片？',
          showCancel: true, //是否显示取消按钮
          cancelText: "否", //默认是“取消”
          cancelColor: '', //取消文字的颜色
          confirmText: "是", //默认是“确定”
          confirmColor: '', //确定文字的颜色
          success: function(res) {
              if (res.cancel) {
                  //点击取消,默认隐藏弹框
              } else {
                  //点击确定
                  that.data.image.splice(dex, 1),
                  that.setData({
                      image: that.data.image
                  })
              }
          },
          fail: function(res) {}, //接口调用失败的回调函数
          complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
      })
  },
  // bindDateChange: function (e) {
  //   this.setData({
  //     date: e.detail.value
  //   })
  // },
  formSubmit: function (e) {
    var that = this
    var name = e.detail.value.name
    if (name == '') {
        wx.showModal({
            title: '提示',
            content: '请填写选题名称！',
            success: function(res) {}
        })
        return
    }
    var company = e.detail.value.company
    if (company == '') {
      wx.showModal({
          title: '提示',
          content: '请填写申报单位！',
          success: function(res) {}
      })
      return
    }
    var content = e.detail.value.content
    if (content == '') {
      wx.showModal({
          title: '提示',
          content: '请填写选题主要内容（不少于150字）！',
          success: function(res) {}
      })
      return
    }
    if (content.length < 150) {
      wx.showModal({
          title: '提示',
          content: '请填写选题主要内容（不少于150字）！',
          success: function(res) {}
      })
      return
    }
    var highlight = e.detail.value.highlight
    if (highlight == '') {
      wx.showModal({
          title: '提示',
          content: '请填写选题亮点！',
          success: function(res) {}
      })
      return
    }
    var report = e.detail.value.report
    if (report == '') {
      wx.showModal({
          title: '提示',
          content: '请填写相关材料或媒体报道！',
          success: function(res) {}
      })
      return
    }
    // if (that.data.files == '') {
    //     wx.showModal({
    //         title: '提示',
    //         content: '请上传相关材料或媒体报道！',
    //         success: function(res) {}
    //     })
    //     return
    // }else{
    //     var report = ''
    //     for(var i=0;i<that.data.files.length;i++){
    //         report += that.data.files[i].src+';'
    //     }
    //     console.log(report)
    // }
    if (that.data.image == '') {
        wx.showModal({
            title: '提示',
            content: '请上传相关图片！',
            success: function(res) {}
        })
        return
    }else{
        var images = ''
        for(var i=0;i<that.data.image.length;i++){
            images += that.data.image[i].img+';'
        }
        console.log(images)
    }
    var shotTime = e.detail.value.shotTime
    if (shotTime == '') {
      wx.showModal({
          title: '提示',
          content: '请填写建议最佳拍摄时间！',
          success: function(res) {}
      })
      return
    }
    var shotAddress = e.detail.value.shotAddress
    if (shotAddress == '') {
      wx.showModal({
          title: '提示',
          content: '请填写拍摄地点！',
          success: function(res) {}
      })
      return
    }
    var shotPerson = e.detail.value.shotPerson
    if (shotPerson == '') {
      wx.showModal({
          title: '提示',
          content: '请填写拍摄基地企业联系人！',
          success: function(res) {}
      })
      return
    }
    var tel = e.detail.value.tel
    if (tel == '') {
      wx.showModal({
          title: '提示',
          content: '请填写电话！',
          success: function(res) {}
      })
      return
    }
    var address = e.detail.value.address
    if (address == '') {
      wx.showModal({
          title: '提示',
          content: '请填写地址！',
          success: function(res) {}
      })
      return
    }
    var email = e.detail.value.email
    if (email == '') {
      wx.showModal({
          title: '提示',
          content: '请填写电子信箱！',
          success: function(res) {}
      })
      return
    }
    var contactName = '';
    var contactPassword = '';
    var name = wx.getStorageSync('contactName')
    var password = wx.getStorageSync('contactPassword')
    if (name != '' && password != '') {
      contactName = name
      contactPassword = password
    }else{
      contactName = e.detail.value.contactName
      if (contactName == '') {
        wx.showModal({
          title: '提示',
          content: '请填写联络员账号！',
          success: function (res) { }
        })
        return
      }
      contactPassword = e.detail.value.contactPassword
      if (contactPassword == '') {
        wx.showModal({
          title: '提示',
          content: '请填写联络员密码！',
          success: function (res) { }
        })
        return
      }
    }
    wx.request({
        url: router,
        data: {
          appKey: 'w4q897jgvxkb',
          method: 'contact.sender.add',
          v: '1.0',
          format: 'json',
          name:name,
          company:company,
          content:content,
          highlight:highlight,
          report:report,
          image:images,
          shotTimeStr:shotTime,
          shotAddress:shotAddress,
          shotPerson:shotPerson,
          tel:tel,
          address:address,
          email:email,
          contactName:contactName,
          contactPassword:contactPassword
        },
        method:"POST",
        header: {
            'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          if (res.statusCode == 200 && res.data.error != 'error') {
            if(res.data.name == '1'){
                wx.showModal({
                    title: '提示',
                    content: '提交成功！',
                    success: function(res) {
                        wx.switchTab({
                            url: '/pages/personal/personal'
                        })
                    }
                })
            }else{
                wx.showModal({
                    title: '提示',
                    content: '提交失败！',
                    success: function(res) {
                        wx.switchTab({
                            url: '/pages/personal/personal'
                        })
                    }
                })
            }
          }
        }
    });
  }
})